package com.hxkcloud.patient.fragments;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.YjhApplication;
import com.hxkcloud.patient.activitys.JoinDepartmentActivity;
import com.hxkcloud.patient.activitys.MessageMainActivity;
import com.hxkcloud.patient.activitys.MyCollectActivity;
import com.hxkcloud.patient.activitys.MyFollowActivity;
import com.hxkcloud.patient.activitys.MyReportExpandListActivity;
import com.hxkcloud.patient.activitys.PersonalInfoForPatientActivity;
import com.hxkcloud.patient.activitys.PregnantInfoActivity;
import com.hxkcloud.patient.activitys.QRcodeScanActivity;
import com.hxkcloud.patient.activitys.SearchRecordActivity;
import com.hxkcloud.patient.activitys.SettingActivity;
import com.hxkcloud.patient.activitys.TakeDrugsActivity;
import com.hxkcloud.patient.activitys.TransactionActivity;
import com.hxkcloud.patient.beans.DepartmentBean;
import com.hxkcloud.patient.dborm.DepartmentDao;
import com.hxkcloud.patient.utils.ImageOptionUtils;
import com.hxkcloud.patient.utils.StringUtil;
import com.hxkcloud.patient.views.CircleImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

import java.util.Calendar;
import java.util.List;

/**
 * Created by ray_lei on 2016/7/14.
 * 我的模块(patient)
 */
public class PatientFragment extends BaseFragment implements View.OnClickListener {
    private View view;
    private LinearLayout titleLayout;

    private LinearLayout ll_title;
    private ImageView iv_search;
    private TextView tv_title;
    private ImageView iv_message;
    private ImageView iv_msgpoint;

    private CircleImageView userIconCI;
    private TextView usernameTV;
    private TextView sexTV;
    private TextView ageTV;

    private LinearLayout personalInfoLayout;
    private LinearLayout infoLayout;
    private LinearLayout collectLayout;
    private LinearLayout followLayout;
    private LinearLayout reportLayout;
    private LinearLayout drugsLayout;
    private LinearLayout scanLayout;
    private LinearLayout kefuLayout;
    private LinearLayout settingLayout;
    //交易.咨询
    private LinearLayout ll_transaction;
    private LinearLayout ll_consult;

    private final int REQUEST_QRCODE = 500;
    private final int REQUEST_JOIN = 501;
    private String departmentId = "";


    private View lineView0;
    private View lineView1;

    private boolean isChanke = false;
    private TextView msg_number;

    public PatientFragment(){
        super();
    }

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {

        }
    }
    private boolean showIcon;

    public void showPoint(boolean show) {
        showIcon=show;
        if (iv_msgpoint != null)
            if (show) {
                iv_msgpoint.setVisibility(View.VISIBLE);
            } else {
                iv_msgpoint.setVisibility(View.INVISIBLE);
            }
        YjhApplication.getInstance().showMsg(msg_number);

    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_patient, container, false);
        initView();
        initData();
        initListener();
        return view;
    }

    @Override
    protected void initView() {
        msg_number = (TextView) view.findViewById(R.id.msg_number);
        ll_title = (LinearLayout) view.findViewById(R.id.ll_title);
        iv_search = (ImageView) view.findViewById(R.id.iv_title_back);
        tv_title = (TextView) view.findViewById(R.id.tv_title_title);
        iv_message = (ImageView) view.findViewById(R.id.iv_title_func);
        iv_msgpoint = (ImageView) view.findViewById(R.id.iv_title_point);

        setTitlePadding(ll_title);
        iv_search.setVisibility(View.INVISIBLE);
        tv_title.setText(getResources().getText(R.string.patient_title_text));
        iv_message.setVisibility(View.VISIBLE);
        iv_message.setImageResource(R.mipmap.nav_message);
        iv_search.setOnClickListener(this);
        iv_message.setOnClickListener(this);


        userIconCI = (CircleImageView) view.findViewById(R.id.patient_ci_usericon);
        usernameTV = (TextView) view.findViewById(R.id.patient_tv_username);
        sexTV = (TextView) view.findViewById(R.id.patient_tv_sex);
        ageTV = (TextView) view.findViewById(R.id.patient_tv_age);

        personalInfoLayout = (LinearLayout) view.findViewById(R.id.patient_layout_pernalinfo);
        infoLayout = (LinearLayout) view.findViewById(R.id.patient_layout_xinxi);
        collectLayout = (LinearLayout) view.findViewById(R.id.patient_layout_collect);
        followLayout = (LinearLayout) view.findViewById(R.id.patient_layout_follow);
        reportLayout = (LinearLayout) view.findViewById(R.id.patient_layout_report);
        drugsLayout = (LinearLayout) view.findViewById(R.id.patient_layout_drugs);
        scanLayout = (LinearLayout) view.findViewById(R.id.patient_layout_scan);
//        kefuLayout = (LinearLayout) view.findViewById(R.id.patient_layout_kefu);
        settingLayout = (LinearLayout) view.findViewById(R.id.patient_layout_setting);

        ll_transaction = (LinearLayout) view.findViewById(R.id.linear_jiaoyi);
        ll_consult  = (LinearLayout) view.findViewById(R.id.linear_zhixun);
        lineView0 = (View) view.findViewById(R.id.patient_view_chanke0);
        lineView1 = (View) view.findViewById(R.id.patient_view_chanke1);
        if (showIcon) {
            iv_msgpoint.setVisibility(View.VISIBLE);
        } else {
            iv_msgpoint.setVisibility(View.INVISIBLE);
        }

    }

    @Override
    protected void initListener() {
        iv_message.setOnClickListener(this);

        personalInfoLayout.setOnClickListener(this);
        infoLayout.setOnClickListener(this);
        collectLayout.setOnClickListener(this);
        followLayout.setOnClickListener(this);
        reportLayout.setOnClickListener(this);
        drugsLayout.setOnClickListener(this);
        scanLayout.setOnClickListener(this);
//        kefuLayout.setOnClickListener(this);
        settingLayout.setOnClickListener(this);
        ll_transaction.setOnClickListener(this);
        ll_consult.setOnClickListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
        DepartmentDao dao = new DepartmentDao(mContext);
        List<DepartmentBean> beans = dao.getDepartment();
        if (beans != null) {
            for (int i = 0; i < beans.size(); i++) {
                if ((beans.get(i).getDepartmentId() + "").equals(loginInfo.getDepartmentId())) {
                    if ("0".equals(beans.get(i).getType())) {
                        isChanke = true;
                        infoLayout.setVisibility(View.VISIBLE);
                        lineView0.setVisibility(View.VISIBLE);
                        lineView1.setVisibility(View.VISIBLE);
                    }
                    break;
                }
            }
        }


        YjhApplication.getInstance().showMsg(msg_number);

    }

    public void setChankeShow() {
        getLoginInfo();
        if (mContext != null && loginInfo != null && infoLayout != null) {
            infoLayout.setVisibility(View.GONE);
            lineView0.setVisibility(View.GONE);
            lineView1.setVisibility(View.GONE);

            DepartmentDao dao = new DepartmentDao(mContext);
            List<DepartmentBean> beans = dao.getDepartment();
            if (beans != null) {
                for (int i = 0; i < beans.size(); i++) {
                    if ((beans.get(i).getDepartmentId() + "").equals(loginInfo.getDepartmentId())) {
                        if ("0".equals(beans.get(i).getType())) {
                            infoLayout.setVisibility(View.VISIBLE);
                            lineView0.setVisibility(View.VISIBLE);
                            lineView1.setVisibility(View.VISIBLE);
                        }
                        break;
                    }
                }
            }
        }
    }

    @Override
    protected void initData() {

        System.out.println("loginInfo====" + loginInfo);
        DisplayImageOptions option = null;
        boolean isYoung = true;
        if (!TextUtils.isEmpty(loginInfo.getAge())) {
            int ageYear = Integer.valueOf(loginInfo.getAge().split("-")[0]);
            Calendar calendar = Calendar.getInstance();
            int curYear = calendar.get(Calendar.YEAR);
            if (curYear - ageYear > 49) {
                isYoung = false;
            } else {
                isYoung = true;
            }
        }
        if (loginInfo.getSex().equals("0")) {//male
            if (!isYoung) {
                option = ImageOptionUtils.getInstance().getMan50HeadOptions();
            } else {
                option = ImageOptionUtils.getInstance().getMan20HeadOptions();
            }
        } else {//female
            if (!isYoung) {
                option = ImageOptionUtils.getInstance().getWoman50HeadOptions();
            } else {
                option = ImageOptionUtils.getInstance().getWoman20HeadOptions();
            }
        }
        Log.i("----headImg---->",loginInfo.getHeadImg());
        imageLoader.displayImage(loginInfo.getHeadImg(), userIconCI, option);
        usernameTV.setText(loginInfo.getName());
        if ("0".equals(loginInfo.getSex())) {
            sexTV.setText(getResources().getString(R.string.male));
        } else if ("1".equals(loginInfo.getSex())) {
            sexTV.setText(getResources().getString(R.string.female));
        }
        if (!"".equals(loginInfo.getAge())) {
            int year = Integer.valueOf(loginInfo.getAge().split("-")[0]);
            int month = Integer.valueOf(loginInfo.getAge().split("-")[1]);
//            Calendar c = Calendar.getInstance();
//            int curYear = c.get(Calendar.YEAR);
            int old = StringUtil.getAgeFromBirthday(year, month);
            if (old == 0) {
                ageTV.setText("<1岁");
            } else {
                ageTV.setText(old + "岁");
            }
        }

        if ("".equals(loginInfo.getDepartmentId())) {
            infoLayout.setVisibility(View.GONE);
            lineView0.setVisibility(View.GONE);
            lineView1.setVisibility(View.GONE);

        }


    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.iv_title_func://消息
                if (!TextUtils.isEmpty(loginInfo.getDepartmentId())) {
                    Intent iM = new Intent(mContext, MessageMainActivity.class);
                    startActivity(iM);
                } else {
                    toastMsg(R.string.pleasefirstjoindepartment);
                }
                break;
            case R.id.patient_layout_pernalinfo:
                StringUtil.zhugeTrack(mContext, "个人信息", "个人信息按钮点击数");

                Intent pernalinfoIntent = new Intent(getActivity(), PersonalInfoForPatientActivity.class);
                startActivity(pernalinfoIntent);
                break;
            case R.id.linear_jiaoyi://交易记录
                Intent transactionIntent = new Intent(getActivity(), TransactionActivity.class);
                startActivity(transactionIntent);

                break;
            case R.id.linear_zhixun://咨询记录
                Intent searchrecordIntent = new Intent(getActivity(), SearchRecordActivity.class);
                startActivity(searchrecordIntent);

                break;

            case R.id.patient_layout_xinxi://孕产信息
                Intent pregnantIntent = new Intent(getActivity(), PregnantInfoActivity.class);
                startActivity(pregnantIntent);
                break;

            case R.id.patient_layout_collect://我的收藏
                StringUtil.zhugeTrack(mContext, "我的收藏", "我的收藏按钮点击数");

                Intent collectIntent = new Intent(getActivity(), MyCollectActivity.class);
                startActivity(collectIntent);
                break;
            case R.id.patient_layout_follow://我的随访
                StringUtil.zhugeTrack(mContext, "我的随访", "我的随访按钮点击数");

                Intent followIntent = new Intent(getActivity(), MyFollowActivity.class);
                startActivity(followIntent);
                break;
            case R.id.patient_layout_report://我的检查报告
//                Intent certIntent = new Intent(getActivity(), InspectionReportActivity.class);
                Intent certIntent = new Intent(getActivity(), MyReportExpandListActivity.class);
                startActivity(certIntent);
                break;
            case R.id.patient_layout_drugs://我的服药记录
//                if (!TextUtils.isEmpty(loginInfo.getDepartmentId())) {
                Intent drugIntent = new Intent(getActivity(), TakeDrugsActivity.class);
                startActivity(drugIntent);
//                } else {
//                    toastMsg(R.string.pleasefirstjoindepartment);
//                }
                break;
            case R.id.patient_layout_scan://扫一扫
                Intent scanIntent = new Intent(getActivity(), QRcodeScanActivity.class);
                startActivityForResult(scanIntent, REQUEST_QRCODE);
                break;
//            case R.id.patient_layout_kefu://在线客服
//
//                break;
            case R.id.patient_layout_setting://设置
                Intent settingIntent = new Intent(getActivity(), SettingActivity.class);
                startActivity(settingIntent);
                break;

        }
    }

    private departmentIdChangeSListener idChangeListener;

    public void setDepartmentIdChangeListener(departmentIdChangeSListener listener) {
        this.idChangeListener = listener;
    }

    //加入或退出科室后，通知fragmentActivity再通知监测页去resume
    public interface departmentIdChangeSListener {
        void onDepartmentIdChange();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == REQUEST_QRCODE) {
                departmentId = data.getStringExtra("result");

                if (!TextUtils.isEmpty(departmentId)) {
                    Intent iIntro = new Intent(mContext, JoinDepartmentActivity.class);
                    iIntro.putExtra("departmentId", departmentId);
                    startActivityForResult(iIntro, REQUEST_JOIN);

//                    loading();
//                    YiJiaHuNetWorkUtils.departmentJoin(departmentId, loginInfo.getUserId(), handler);
                }
            } else if (requestCode == REQUEST_JOIN) {
                boolean isJoin = data.getBooleanExtra("join", false);
                if (isJoin) {
                    if (idChangeListener != null) {
                        idChangeListener.onDepartmentIdChange();
                    }
//                    loading();
//                    YiJiaHuNetWorkUtils.departmentJoin(departmentId, loginInfo.getUserId(), handler);
                    toastMsg(R.string.departmentjoinsuccess);
                    initData();
                } else {

                }
            }
        }
    }
}
